using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Framework
{
    [RequireComponent(typeof(Canvas))]
    public class UGUIWindow : UGUIWidget
    {
        public float DestoryOnCloseWaitTime
        {
            get
            {
                return destroyTime;
            }
        }
        [SerializeField]
        private float destroyTime = -1f;
        [SerializeField]
        private int layer = 0;
        [SerializeField]
        private bool opaqueView;
        [SerializeField]
        private RectTransform[] safeAreaTransform;
        [SerializeField]
        private GameObject[] showState;
        [SerializeField]
        private RectTransform[] scaleTransform;
        public UIWindow Window { get; private set; }
        public Canvas Canvas { get; private set; }
        public GraphicRaycaster GraphicRaycaster { get; private set; }
        private bool showByOrder;
        public int Layer
        {
            get
            {
                return layer;
            }
        }
        public bool Opaque
        {
            get
            {
                return opaqueView;
            }
        }
        public bool Working
        {
            get
            {
                if (_workingActors == null) return false;
                return _workingActors.Count > 0;
            }
        }
        private List<object> _workingActors;
        private object _sign;
        private Action _closeAction;
        protected override void OnAwake()
        {
            base.OnAwake();
            showByOrder = false;
            Canvas = GetComponent<Canvas>();
            Canvas.overrideSorting = true;
            GraphicRaycaster = GetComponent<GraphicRaycaster>();
            if (this.Widget != null)
            {
                if (this.Widget is UIWindow window)
                {
                    this.Window = window;
                }
            }
            if (showState != null)
            {
                foreach (var item in showState)
                {
                    Utils.Unity.SetActive(item, false);
                }
            }
        }
        //csharp没有友元方法,用这种方法模拟权限
        public void SetSign(object sign)
        {
            if (_sign != null) return;
            _sign = sign;
            if (this.Window != null)
            {
                this.Window.Awake(_sign);
            }
        }
        public void SetCloseAction(object sign, Action value)
        {
            if (_sign != null && _sign != sign)
            {
                FrameworkLog.LogError("权限出错");
                return;
            }
            _closeAction = value;
        }
        public void SetSortingOrder(int value)
        {
            var old = Canvas.sortingOrder;
            if (old == value) return;
            Canvas.sortingOrder = value;
            if (this.Window != null)
            {
                this.Window.OnSortingOrderChanged(value, old);
            }
        }
        public void SetScreenInfo(object sign, UGUIScreenInfo value)
        {
            if (_sign != null && _sign != sign)
            {
                FrameworkLog.LogError("权限出错");
                return;
            }
            if (safeAreaTransform != null)
            {
                foreach (var item in safeAreaTransform)
                {
                    if (item == null) continue;
                    item.anchorMin = value.SafeAreaAnchorMin;
                    item.anchorMax = value.SafeAreaAnchorMax;
                    item.sizeDelta = Vector2.zero;
                    item.anchoredPosition = Vector2.zero;
                }
            }
            if (scaleTransform != null && scaleTransform.Length > 0)
            {
                var s = 1f / value.RootScale;
                var scale = new Vector3(s, s, 1f);
                foreach (var item in scaleTransform)
                {
                    if (item == null) continue;
                    item.localScale = scale;
                }
            }
            if (this.Window != null)
            {
                this.Window.SetScreenInfo(_sign, value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="showOrder">最大显示的sortingOrder</param>
        /// <returns>当前是否显示状态</returns>
        public UIStatus SetShowSortingOrder(int showOrder)
        {
            var value = Canvas.sortingOrder;
            bool show = value >= showOrder;
            Canvas.enabled = show;
            if (show != showByOrder)
            {
                showByOrder = show;
                if (this.Window != null)
                {
                    if (show)
                    {
                        this.Window.Focus(_sign);
                        return UIStatus.Open;
                    }
                    else
                    {
                        this.Window.Blur(_sign);
                        return UIStatus.Close;
                    }
                }
            }
            return UIStatus.Idle;
        }
        public void SetWorking(object actor, bool working)
        {
            if (!working)
            {
                if (_workingActors != null)
                {
                    _workingActors.Remove(actor);
                }
            }
            else
            {
                if (_workingActors == null)
                {
                    _workingActors = new List<object>(3);
                }
                if (_workingActors.Contains(actor)) return;
                _workingActors.Add(actor);
            }
            if (GraphicRaycaster != null)
            {
                GraphicRaycaster.enabled = !Working;
            }
        }
        public void ClearWorking()
        {
            if (_workingActors != null)
            {
                _workingActors.Clear();
            }
            if (GraphicRaycaster != null)
            {
                GraphicRaycaster.enabled = !Working;
            }
        }
        public void SetStatus(object sign, UIStatus value, UIStatus previous)
        {
            if (_sign != null && _sign != sign)
            {
                FrameworkLog.LogError("权限出错");
                return;
            }
            if (showState != null)
            {
                if (value == UIStatus.Open)
                {
                    foreach (var item in showState)
                    {
                        Utils.Unity.SetActive(item, true);
                    }
                }

                if (value == UIStatus.CloseAfter)
                {
                    foreach (var item in showState)
                    {
                        Utils.Unity.SetActive(item, false);
                    }
                }
            }
            if (this.Window != null)
            {
                this.Window.SetStatus(_sign, value, previous);
            }
            if (value == UIStatus.Close)
            {
                if (this.Window.IsFocus)
                {
                    this.Window.Blur(_sign);
                }
            }

        }
        public void SetParam(string name, object value)
        {
            if (this.Window == null) return;
            this.Window.SetParam(name, value);
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            if (_workingActors != null)
            {
                _workingActors.Clear();
                _workingActors = null;
            }
            this.Window = null;
            _sign = null;
            _closeAction = null;
        }
        public void Close()
        {
            _closeAction?.Invoke();
        }

    }
}